package file;

import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 25397
 * Date: 2022-04-11
 * Time: 20:56
 */
public class Demo10 {
    //按照字符流来读写
    public static void main(String[] args) {
        try(Reader reader=new FileReader("d:/test.txt")){
            //按字符来读
            while(true){
                char[] buffer=new char[1024];
                int len=reader.read(buffer);//不一定能读满1024
                if(len==-1){
                    break;
                }
                //法一
                for(int i=0;i<len;i++){
                    System.out.print(buffer[i]);//打印abc
                }

                System.out.println("========分割线==========");

                //法二
                String str=new String(buffer,0,len);//从下标0读len个
                //如果这里传入的数组是byte数组，还可以手动指定utf-8,字符集来避免乱码
                System.out.println(str);//打印abc
            }
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
